package copyengine.ui.component.progressbar.animation
{
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.geom.Rectangle;

	public class CEProgressNoEffectAnimation implements ICEProgressBarAnimation
	{
		private static const PROGRESS_DIRECTION_HORIZONTAL:int = 1;
		private static const PROGRESS_DIRECTION_VERTICAL:int = 2;

		private var direction:int;
		private var progressMask:Shape;
		private var progressSize:Number;
		private var thumb:MovieClip;
		private var originalThumbPos:int;

		public function CEProgressNoEffectAnimation()
		{
		}

		public function setTarget(_progressBarMc:MovieClip):void
		{
			var progressMc:MovieClip = _progressBarMc["progress"];
			var rect:Rectangle = progressMc.getBounds(progressMc);
			if (_progressBarMc["thumb"])
			{
				thumb = _progressBarMc["thumb"];
			}

			progressMask = new Shape();
			if (rect.width > rect.height)
			{
				progressMask.graphics.beginFill(0, 0);
				progressMask.graphics.drawRect(0, 0, rect.width, rect.height);
				progressMask.graphics.endFill();
				progressMask.x = rect.left;
				progressMask.y = rect.top;

				progressSize = rect.width;
				direction = PROGRESS_DIRECTION_HORIZONTAL;

				if (thumb)
				{
					originalThumbPos = thumb.x;
				}
			}
			else
			{
				progressMask.graphics.beginFill(0, 0);
				progressMask.graphics.drawRect(0, -rect.height, rect.width, rect.height);
				progressMask.graphics.endFill();
				progressMask.x = rect.left;
				progressMask.y = rect.bottom;

				progressSize = rect.height;
				direction = PROGRESS_DIRECTION_VERTICAL;

				if (thumb)
				{
					originalThumbPos = thumb.y;
				}
			}
			progressMc.addChild(progressMask);
			progressMc.mask = progressMask;
		}

		/**
		 * @param _percent
		 */
		public function setProgressPercent(_percent:Number):void
		{
			//Thumb的移动方式
			//如果是水平的则从左到右，如果是垂直的则从上到下
			if (direction == PROGRESS_DIRECTION_HORIZONTAL)
			{
				progressMask.width = progressSize * _percent * 0.01;
				if (thumb)
				{
					thumb.x = originalThumbPos + progressSize * _percent * 0.01;
				}
			}
			else
			{
				progressMask.height = progressSize * _percent * 0.01;
				if (thumb)
				{
					thumb.y = originalThumbPos + progressSize * (1 - _percent * 0.01);
				}
			}
		}

		public function dispose():void
		{
			progressMask = null;
		}
	}
}
